import sinon from 'sinon';
import {expect} from 'chai';
// @ts-ignore
import * as outputs from '../../src/outputs';
// @ts-ignore
import {
    ArtifactDocker, CustomArtifact,
    CustomArtifactDocker,
    exportArtifactDocker, exportCustomArtifact,
    exportCustomArtifactDocker
} from '../../src/artifact/artifact';

describe('addArtifactDocker functions', () => {
    let sandbox: sinon.SinonSandbox;
    let addOutputStub: sinon.SinonStub;

    before(() => {
        sandbox = sinon.createSandbox();
    });

    beforeEach(() => {
        addOutputStub = sandbox.stub(outputs, 'addOutput');
    });

    afterEach(() => {
        sandbox.restore();
    });

    describe('addArtifactDocker', () => {
        it('should call addOutput with the correct artifact JSON for non-VPC', () => {
            const stepID = 'testStepID';
            const artifact = new ArtifactDocker({
                artifact: 'my_image',
                image: 'registry/namespace/repo:tag',
                dockerTag: 'tag'
            });

            exportArtifactDocker(`${stepID}.artifact`, artifact);
            sinon.assert.calledWith(addOutputStub, `${stepID}.artifact=${JSON.stringify(artifact)}`);
        });
    });

    describe('addArtifactDocker VPC', () => {
        it('should call addOutput with the correct artifact JSON for VPC', () => {
            const stepID = 'testStepID';
            const artifact = new ArtifactDocker({
                artifact: 'my_image',
                image: 'registry-vpc/namespace/repo:tag',
                dockerTag: 'tag'
            });

            exportArtifactDocker(`${stepID}.artifact_vpc`, artifact);
            sinon.assert.calledWith(addOutputStub, `${stepID}.artifact_vpc=${JSON.stringify(artifact)}`);
        });
    });
});

describe('customArtifact functions', () => {
    let sandbox: sinon.SinonSandbox;
    let addOutputStub: sinon.SinonStub;

    before(() => {
        sandbox = sinon.createSandbox();
    });

    beforeEach(() => {
        addOutputStub = sandbox.stub(outputs, 'addOutput');
    });

    afterEach(() => {
        sandbox.restore();
    });

    describe('exportCustomArtifactDocker', () => {
        it('should call addOutput with the correct artifact JSON for customArtifactDocker', () => {
            process.env.stepIdentifier = "test_step_id"

            const artifact = new CustomArtifactDocker({
                dockerURL: 'registry/namespace/repo:tag',
            });

            exportCustomArtifactDocker(`artifactName`, artifact);
            sinon.assert.calledWith(addOutputStub, `test_step_id.artifactName=${JSON.stringify(artifact)}`);

            delete process.env.stepIdentifier
        });
    });

    describe('exportCustomArtifact', () => {
        it('should call addOutput with the correct artifact JSON for CustomArtifact', () => {
            process.env.stepIdentifier = "test_step_id"

            const artifact = new CustomArtifact({
                artifactURL: "fake_artifact_url"
            });

            exportCustomArtifact(`artifactName`, artifact);
            sinon.assert.calledWith(addOutputStub, `test_step_id.artifactName=${JSON.stringify(artifact)}`);

            delete process.env.stepIdentifier
        });
    });
});